package initialize

import (
	"github.com/gin-gonic/gin"

	"awesomeProject/app/handler"
	"awesomeProject/app/middleware"
)

type Handlers struct {
	UserHandler              handler.UserHandler
	RoleHandler              handler.RoleHandler
	PermissionHandler        handler.PermissionHandler
	MenuHandler              handler.MenuHandler
	CasbinHandler            handler.CasbinHandler
	AccountHandler           handler.AccountHandler
	ShopHandler              handler.ShopHandler
	ShopTransHandler         handler.ShopTransHandler
	ShopTrendHandler         handler.ShopTrendHandler
	ShopTrafficHandler       handler.ShopTrafficHandler
	OrderHandler             handler.OrderHandler
	OrderOverviewHandler     handler.OrderOverviewHandler
	OrderItemOverViewHandler handler.OrderItemOverViewHandler
	OrderItemCurrentHandler  handler.OrderItemCurrentHandler
	ShipmentHandler          handler.ShipmentHandler
	InboundPlanHandler       handler.InboundPlanHandler
	RefundHandler            handler.RefundHandler
	PlaceHandler             handler.DeliverPlaceHandler
	ItemsIndicateHandler     handler.ItemsIndicateHandler
	ItemFlowRankHandler      handler.ItemFlowRankHandler
	ShopFlowSourceHandler    handler.ShopFlowSourceHandler
	CommentHandler           handler.CommentHandler
	StatisticsHandler        handler.StatisticsHandler
	ShopDataHandler          handler.ShopDataHandler
	AfterSaleHandler         handler.AfterSaleHandler
	WareHorseHandler         handler.WareHorseHandler
	CompanyHandler           handler.CompanyHandler
	ShopStatisticsHandler    handler.ShopStatisticsHandler
	LoginlogHandler          handler.LoginlogHandler
	PurchaseHandler          handler.PurchaseHandler
	OrderProcessHandler      handler.OrderProcessHandler
	ShipmentProcessHandler   handler.ShipmentProcessHandler
	RefundProcessHandler     handler.RefundProcessHandler
	HostHandler              handler.HostHandler
}

func RouterInit() *gin.Engine {
	var r = gin.New()
	gin.SetMode(gin.ReleaseMode)
	var handlers = new(Handlers)
	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"msg": "pong",
		})
	})

	r.GET("export", handlers.CommentHandler.Export)

	// 公共路由（不做鉴权）
	publicGroup := r.Group("")
	{
		publicGroup.POST("user/login", handlers.UserHandler.Login)
		publicGroup.POST("user/logout", handlers.UserHandler.Logout)
		// 地区数据
		publicGroup.GET("places", handlers.PlaceHandler.QueryList)
	}

	// 当前登录用户路由
	authRouter := r.Group("")
	authRouter.Use(middleware.Authorize())
	{
		authRouter.GET("user_menus", handlers.UserHandler.Menus)
		authRouter.GET("user_detail", handlers.UserHandler.UserDetail)
		authRouter.PUT("user_password_change", handlers.UserHandler.PasswordChange)
		authRouter.PUT("user_update", handlers.UserHandler.CurrentUpdate)
	}

	// 超管路由
	sysRouter := r.Group("")
	sysRouter.Use(middleware.Authorize(), middleware.RBACHandler())
	{
		// 权限
		sysRouter.GET("permissions", handlers.PermissionHandler.Pagination)
		sysRouter.GET("permission_groups", handlers.PermissionHandler.Groups)
		sysRouter.POST("permission", handlers.PermissionHandler.Create)
		sysRouter.GET("permission/:id", handlers.PermissionHandler.Show)
		sysRouter.PUT("permission/:id", handlers.PermissionHandler.Update)
		sysRouter.DELETE("permission/:id", handlers.PermissionHandler.Destroy)
		sysRouter.DELETE("permission/:id/del", handlers.PermissionHandler.Del)

		// 菜单
		sysRouter.POST("menu", handlers.MenuHandler.Create)
		sysRouter.GET("menu/:id", handlers.MenuHandler.Show)
		sysRouter.PUT("menu/:id", handlers.MenuHandler.Update)
		sysRouter.DELETE("menu/:id", handlers.MenuHandler.Del)

		// 角色
		sysRouter.GET("roles", handlers.RoleHandler.Pagination)
		sysRouter.POST("role", handlers.RoleHandler.Create)
		sysRouter.PUT("role/:id", handlers.RoleHandler.Update)
		sysRouter.GET("role/:id", handlers.RoleHandler.Show)
		sysRouter.DELETE("role/:id", handlers.RoleHandler.Del)
		sysRouter.POST("role/:id/permissions", handlers.CasbinHandler.AddPermissionsForRole)
		sysRouter.GET("role/:id/permissions", handlers.RoleHandler.Permissions)
		sysRouter.GET("role/:id/menus", handlers.RoleHandler.Menus)
		sysRouter.POST("role/:id/menus", handlers.RoleHandler.AddMenusForRole)

		// 账户
		sysRouter.GET("accounts", handlers.AccountHandler.Pagination)
		sysRouter.POST("account", handlers.AccountHandler.Create)
		sysRouter.GET("account/:id", handlers.AccountHandler.Show)
		sysRouter.PUT("account/:id", handlers.AccountHandler.Update)
		sysRouter.DELETE("account/:id", handlers.AccountHandler.Del)
		sysRouter.POST("account/hash_pwd", handlers.AccountHandler.GetHashPwd)
		sysRouter.POST("account/account", handlers.AccountHandler.GetAccount)

		// 组织管理
		sysRouter.GET("companies", handlers.CompanyHandler.Pagination)
		sysRouter.POST("company", handlers.CompanyHandler.Create)
		sysRouter.GET("company/:id", handlers.CompanyHandler.Show)
		sysRouter.PUT("company/:id", handlers.CompanyHandler.Update)
		sysRouter.DELETE("company/:id", handlers.CompanyHandler.Del)

		// 用户
		sysRouter.GET("users", handlers.UserHandler.Pagination)
		sysRouter.POST("user", handlers.UserHandler.Create)
		sysRouter.GET("user/:id", handlers.UserHandler.Show)
		sysRouter.DELETE("user/:id", handlers.UserHandler.Del)
		sysRouter.PUT("user/:id", handlers.UserHandler.Update)
		sysRouter.PUT("user/password_reset", handlers.UserHandler.PasswordReset)
		sysRouter.PUT("user/ip_limit", handlers.UserHandler.UpdateIpLimit)
		sysRouter.GET("user/all_limit", handlers.UserHandler.GetAllLimit)
		sysRouter.PUT("user/all_limit", handlers.UserHandler.UpdateAllLimit)
		sysRouter.POST("user/:id/roles", handlers.CasbinHandler.AddRolesForUser)
		sysRouter.GET("user/:id/roles", handlers.CasbinHandler.UserRoles)
		sysRouter.PUT("user/:id/shops", handlers.UserHandler.AddShopsForUser)
		sysRouter.GET("user/:id/shops", handlers.UserHandler.UserShops)
		sysRouter.GET("user/:id/company", handlers.UserHandler.Company)

		// 登录日志
		sysRouter.GET("log", handlers.LoginlogHandler.Pagination)
		sysRouter.GET("log/:id", handlers.LoginlogHandler.Show)
		sysRouter.DELETE("log/:id", handlers.LoginlogHandler.Del)

		// 主机（账户关联主机）
		sysRouter.GET("hosts", handlers.HostHandler.Pagination)
		sysRouter.POST("host", handlers.HostHandler.Create)
		sysRouter.GET("host/:id", handlers.HostHandler.Show)
		sysRouter.PUT("host/:id", handlers.HostHandler.Update)
		sysRouter.DELETE("host/:id", handlers.HostHandler.Del)
		sysRouter.GET("host/states", handlers.HostHandler.HostStatesPagination)
		sysRouter.POST("host/reboot", handlers.HostHandler.HostReboot)
	}

	shopAuthRouter := r.Group("")
	shopAuthRouter.Use(middleware.Authorize(), middleware.ShopAuth())
	{
		shopAuthRouter.GET("company_map", handlers.CompanyHandler.CompanyMap)
		shopAuthRouter.GET("menus", handlers.MenuHandler.MenuTree)
		shopAuthRouter.GET("shop_map", handlers.ShopHandler.QueryMap)
		shopAuthRouter.GET("shop_map_full", handlers.ShopHandler.QueryMapFull)
	}

	// 业务路由
	businessGroup := r.Group("")
	businessGroup.Use(middleware.Authorize(), middleware.RBACHandler(), middleware.ShopAuth())
	{
		// 店铺相关
		businessGroup.GET("shop/trend", handlers.ShopTrendHandler.Trend)
		businessGroup.GET("shop/first_traffic_trend", handlers.ShopTrafficHandler.FirstTrafficTrend)
		businessGroup.GET("shop/second_traffic_rank", handlers.ShopTrafficHandler.SecondTrafficRank)
		businessGroup.GET("shop/trans", handlers.ShopTransHandler.ShopTrans)

		// 订单
		businessGroup.GET("orders", handlers.OrderHandler.Pagination)
		businessGroup.GET("order/status_count", handlers.OrderHandler.StatusCount)
		businessGroup.GET("order/overview", handlers.OrderOverviewHandler.Overview)
		businessGroup.GET("order/item_overview", handlers.OrderItemOverViewHandler.ItemOverView)
		businessGroup.GET("order/item_current", handlers.OrderItemCurrentHandler.ItemCurrent)
		businessGroup.GET("order/export", handlers.OrderHandler.Export)

		businessGroup.GET("purchase_types", handlers.PurchaseHandler.PurchaseTypesMap)
		businessGroup.GET("purchases", handlers.PurchaseHandler.Pagination)
		businessGroup.POST("purchases", handlers.PurchaseHandler.Create)
		businessGroup.GET("purchases/:id", handlers.PurchaseHandler.Show)
		businessGroup.PUT("purchases/:id", handlers.PurchaseHandler.Update)
		businessGroup.DELETE("purchases/:id", handlers.PurchaseHandler.Del)
		businessGroup.PUT("purchase_change/:id/:is_default", handlers.PurchaseHandler.ChangeDefault)

		businessGroup.GET("order_process", handlers.OrderProcessHandler.Pagination)
		businessGroup.PUT("order_process", handlers.OrderProcessHandler.Update)

		// 货件
		businessGroup.GET("shipments", handlers.ShipmentHandler.Pagination)
		businessGroup.GET("shipment/detail_view", handlers.ShipmentHandler.DetailView)
		businessGroup.GET("shipment/track_box_view", handlers.ShipmentHandler.ShipmentTrackBoxView)
		businessGroup.GET("shipment/received_detail_view", handlers.ShipmentHandler.ReceivedDetailView)
		businessGroup.GET("shipment/box_view", handlers.ShipmentHandler.BoxView)
		businessGroup.GET("inbound_plans", handlers.InboundPlanHandler.Pagination)
		//businessGroup.GET("shipment/:id", handlers.ShipmentHandler.Show)

		businessGroup.GET("shipment_process", handlers.ShipmentProcessHandler.Pagination)
		businessGroup.PUT("shipment_process", handlers.ShipmentProcessHandler.Update)

		// 退换货
		businessGroup.GET("refunds", handlers.RefundHandler.Pagination)
		//businessGroup.GET("refund_confirmed", handlers.RefundHandler.RefundConfirmed)
		businessGroup.GET("refund/:id", handlers.RefundHandler.Show)
		businessGroup.GET("refund/:id/img", handlers.RefundHandler.Img)
		businessGroup.GET("refund_detail", handlers.RefundHandler.Detail)

		businessGroup.GET("refund_process", handlers.RefundProcessHandler.Pagination)
		businessGroup.PUT("refund_process", handlers.RefundProcessHandler.Update)

		// 商品
		businessGroup.GET("items_indicates", handlers.ItemsIndicateHandler.Pagination)

		// 流量统计
		businessGroup.GET("item_flow_rank", handlers.ItemFlowRankHandler.ItemFlowRank)
		businessGroup.GET("shop_flow_source", handlers.ShopFlowSourceHandler.ShopFlowSource)

		// 评价中心
		businessGroup.GET("comments", handlers.CommentHandler.Pagination)
		businessGroup.GET("comment/dimension_count", handlers.CommentHandler.DimensionCount)
		businessGroup.GET("comment/export", handlers.CommentHandler.Export)

		// 仪表盘
		businessGroup.GET("statistics/shop", handlers.ShopStatisticsHandler.Single)
		// 统计汇总
		businessGroup.GET("statistics/shop_sale", handlers.ShopStatisticsHandler.SaleStatistics)
		businessGroup.GET("statistics/shop_item_sale", handlers.ShopStatisticsHandler.ItemSaleStatistics)
		businessGroup.GET("statistics/summary", handlers.ShopStatisticsHandler.Summary)
	}

	return r
}
